package com.shop.controller;

import com.shop.pojo.DtsAdmin;
import com.shop.pojo.DtsComment;
import com.shop.service.DtsBrandService;
import com.shop.service.DtsCommentService;
import com.shop.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName AdminCommentController
 * @Author xiao宋
 * @Date 10:12  2022/10/22
 * @Version 1.0
 * @Description TODO
 */
@RestController
@RequestMapping("/admin/comment")
public class AdminCommentController {
    @Autowired
    DtsBrandService brandService;
    @Autowired
    DtsCommentService commentService;

    /**
     * 删除评论
     *
     * @param comment
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsComment comment) {
        commentService.delete(comment.getId());
        return ResponseUtil.ok();
    }

    /**
     * 显示评论列表
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param userId
     * @param goodsId
     * @return
     */
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
            @RequestParam(value = "sort", required = false, defaultValue = "add_time") String sort,
            @RequestParam(value = "order", required = false, defaultValue = "desc") String order,
            Integer userId,
            @RequestParam(value = "valueId", required = false) Integer goodsId
    ) {
        List<Integer> brandIds = null;
        Subject subject = SecurityUtils.getSubject();
        if (subject.hasRole(("品牌制造商"))) {
            DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
            brandIds = brandService.findBrandIdsByAdminId(admin.getId());
        }
        Map<String, Object> data = commentService.findByPage(page, limit, sort, order, goodsId, userId, brandIds);
        return ResponseUtil.ok(data);
    }
}
